package
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import it.gotoandplay.smartfoxserver.SFSEvent;

	public class ShopScreen extends Shop
	{
		private var tireShop:TireShop;
		private var engineShop:EnginesShop;
						
		public function ShopScreen(game:DRace)
		{
			super();
			//creo los menues ya
			tireShop = new TireShop(game,this);
			engineShop = new EnginesShop(game,this);
						
			//close events	
			header.btnClose.addEventListener(MouseEvent.CLICK,game.onShopClosed);
			header.btnClose.addEventListener(MouseEvent.ROLL_OVER,onCloseRollOver);
			header.btnClose.addEventListener(MouseEvent.ROLL_OUT,onCloseRollOut);
							
			//buttons
			btnTires.onEventClick = onTireShopButtonClick;
			btnEngines.onEventClick = onEnginesShopButtonClick;
			//btnEngines.onEventClick = onMouseClick;
			//btnPaint.onEventClick = onMouseClick;
			
			addEventListener(Event.ADDED_TO_STAGE,onAddedToStage);
			
		}
		private function onAddedToStage(ev:Event):void
		{
			//getPlayerFromDataBase();
			//por ahora lo pongo aca despues raja y iria en los set o algo
			carStatus.userName.text = DRace.player.userName;
			
			carStatus.acel.value.text = DRace.player.car.engine.acel.toString();
			carStatus.maxSpeed.value.text = DRace.player.car.engine.maxSpeed.toString();
			carStatus.grip.value.text = DRace.player.car.tire.grip.toString();
			
			carStatus.newAcel.value.text = carStatus.acel.value.text;
			carStatus.newMaxSpeed.value.text = carStatus.maxSpeed.value.text;
			carStatus.newGrip.value.text = carStatus.grip.value.text;
			
			carStatus.currentMoneyValue.text = DRace.player.money.toString();
			carStatus.sellPriceValue.text = "0";
			carStatus.afterBuyValue.text = carStatus.currentMoneyValue.text;
		}
		private function getPlayerFromDataBase():void
		{
		//	DRace.sfc.addEventListener(SFSEvent.onExtensionResponse,onExtensionResponse);
		//	DRace.sfc.sendXtMessage("simpleExt","getPlayer",DRace.player);
			
		}
		private function onExtensionResponse(ev:SFSEvent):void
		{
		//	trace (ev);
		}
		public function setLanguage(xmlFile:XML):void
		{
			header.label.text = xmlFile.ShopScreen.attribute("header");
			btnEngines.setText(xmlFile.ShopScreen.attribute("btnEngines"));
			btnPaint.setText(xmlFile.ShopScreen.attribute("btnPaint"));
			btnTires.setText(xmlFile.ShopScreen.attribute("btnTires"));
			
			carStatus.acel.label.text = xmlFile.ShopScreen.CarStatus.attribute("acel");
			carStatus.maxSpeed.label.text = xmlFile.ShopScreen.CarStatus.attribute("maxSpeed");
			carStatus.grip.label.text = xmlFile.ShopScreen.CarStatus.attribute("grip");
			
			carStatus.newAcel.label.text = xmlFile.ShopScreen.CarStatus.attribute("newAcel");
			carStatus.newMaxSpeed.label.text = xmlFile.ShopScreen.CarStatus.attribute("newMaxSpeed");
			carStatus.newGrip.label.text = xmlFile.ShopScreen.CarStatus.attribute("newGrip");
			
			carStatus.currentMoneyLabel.text = xmlFile.ShopScreen.CarStatus.attribute("currentMoneyLabel");
			
			tireShop.setLanguage(xmlFile);
			engineShop.setLanguage(xmlFile);
		}
		private function onTireShopButtonClick(ev:MouseEvent):void
		{
			if (shopCanvas.contains(tireShop))
				shopCanvas.removeChild(tireShop);
			else
				shopCanvas.addChild(tireShop);
		}
		private function onEnginesShopButtonClick(ev:MouseEvent):void
		{
			if (shopCanvas.contains(engineShop))
				shopCanvas.removeChild(engineShop);
			else
				shopCanvas.addChild(engineShop);
			
		}
		private function onCloseRollOver(ev:MouseEvent):void
		{
			header.btnClose.textColor = 0xFFFFFF;
		}
		private function onCloseRollOut(ev:MouseEvent):void
		{
			header.btnClose.textColor = 0xFF0000;
		}
		
		public function update():void
		{
			if (contains(tireShop))
				tireShop.update();
			else (contains(engineShop))
			{
				engineShop.update();
				
			}
		}
		public function changeStatusTire(tire:Tire):void
		{
			if (tire)
			{
				carStatus.newGrip.value.text = tire.grip.toString();
				
				if (DRace.player.car.tire.grip < tire.grip)
					carStatus.newGrip.value.textColor = 0x00FF00;
				else
					carStatus.newGrip.value.textColor = 0xFF0000;
					
				carStatus.buyValue.text = tire.price.toString();
			}
			else
			{
				carStatus.buyValue.text = "0";
				carStatus.newGrip.value.text =	DRace.player.car.tire.grip.toString();
				carStatus.newGrip.value.textColor = 0x000000;
			}
			updateMoneyInGui("Tire");
		}
		public function changeStatusEngine(engine:Engine):void
		{
			if (engine)
			{
				carStatus.newAcel.value.text = engine.acel.toString();
				carStatus.newMaxSpeed.value.text = engine.maxSpeed.toString();
				
				if (DRace.player.car.engine.maxSpeed < engine.maxSpeed)
					carStatus.newMaxSpeed.value.textColor = 0x00FF00;
				else
					carStatus.newMaxSpeed.value.textColor = 0xFF0000;
				
				if (DRace.player.car.engine.acel < engine.acel)
					carStatus.newAcel.value.textColor = 0x00FF00;
				else
					carStatus.newAcel.value.textColor = 0xFF0000;
					
				carStatus.buyValue.text = engine.price.toString();
			
			}
			else
			{
				carStatus.buyValue.text = "0";
				
				carStatus.newAcel.value.text =	DRace.player.car.engine.acel.toString();
				carStatus.newAcel.value.textColor = 0x000000;
				
				carStatus.newMaxSpeed.value.text =	DRace.player.car.engine.maxSpeed.toString();
				carStatus.newMaxSpeed.value.textColor = 0x000000;
			}
			updateMoneyInGui("Engine");
			
		}
		public function updateMoneyInGui(obj:String):void
		{
			var buyValue:int = int(carStatus.buyValue.text);
			
			var sellPrice:int;
			if (obj == "Tire")
				sellPrice = DRace.player.car.tire.price / 2;
			else if (obj == "Engine")
				sellPrice = DRace.player.car.engine.price /2 ;
				
			carStatus.sellPriceValue.text = sellPrice.toString();
		
			var result:int = DRace.player.money + sellPrice - buyValue;
			carStatus.afterBuyValue.text = result.toString();
			
		}
		public function changeStatusPlayer():void
		{
			carStatus.acel.text = DRace.player.car.engine.acel.toString();
			carStatus.maxSpeed.text = DRace.player.car.engine.maxSpeed.toString();
			carStatus.grip.text = DRace.player.car.tire.grip.toString();
			carStatus.currentMoneyValue.text = DRace.player.money.toString();
		
		}
		
		
	}
}